package com.mall.manager.authority.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.mall.manager.authority.service.PowerService;
import com.mall.untils.Page;
import com.mall.untils.StringUtil;

/**
 * 权限
 * 
 * @ClassName: PowerController
 * @Description:
 * @author:wangwenyue wangwenyue@ide365.com
 * @date 2016年7月7日 下午2:53:18
 */
@Controller
@RequestMapping("/manager")
public class PowerController {
	@Resource
	private PowerService powerService;
	
	

	/**
	 * 获取父级别的权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getParent")
	@ResponseBody
	public String getParent(HttpServletRequest request){
		List<Map<String,Object>> res = powerService.getParent(WebUtils.getParametersStartingWith(request, "").get("level").toString());
		JSONArray jsonArray = JSONArray.fromObject(res);
		return String.valueOf(jsonArray);
	}
	
	
	
	
	/**
	 * 跳转添加或修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/power_edit")
	public String power_edit(HttpServletRequest request, ModelMap modelMap) {
		Map<String, Object> map = WebUtils.getParametersStartingWith(request, "");
		String idStr = String.valueOf(map.get("id"));
		if(StringUtils.isNotBlank(idStr) && !"null".equals(idStr)){
			map.put("claim", "id");
			map.put("res", idStr);
			Map<String, Object> powerByclaim = powerService.getPowerByclaim(map);
			modelMap.addAttribute("id", powerByclaim.get("id"));
			modelMap.addAttribute("name", powerByclaim.get("name"));
			modelMap.addAttribute("poweraddr", powerByclaim.get("poweraddr"));
			modelMap.addAttribute("level", powerByclaim.get("level"));
			modelMap.addAttribute("remarks", powerByclaim.get("remarks"));
			modelMap.addAttribute("parent_id", powerByclaim.get("parent_id"));
			if("2".equals(powerByclaim.get("level").toString())){
				List<Map<String, Object>> parent = powerService.getParent("1");
				modelMap.addAttribute("parents",parent);
			}
			if("3".equals(powerByclaim.get("level").toString())){
				List<Map<String, Object>> parent = powerService.getParent("2");
				modelMap.addAttribute("parents",parent);
			}
		}
		return "view/limits_add";
	}
	
	@RequestMapping(value = "/power_save", method=RequestMethod.POST)
	public ModelAndView power_save(HttpServletRequest request, ModelMap modelMap){
		Map<String, Object> map = WebUtils.getParametersStartingWith(request, "");
		powerService.addPower(map);
		return new ModelAndView("redirect:/manager/power_queryPage?currentpageno=1");
	}
	
	
	
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/power_remove")
	public ModelAndView power_remove(HttpServletRequest request, ModelMap modelMap) {
		powerService.removePower(WebUtils.getParametersStartingWith(request, ""));
		return new ModelAndView("redirect:/manager/power_queryPage?currentpageno=1");
	}
	
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/power_queryPage")
	public String power_queryPage(HttpServletRequest request, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		Page page = powerService.queryPagelist(paraMap);
		
		if(null!=page){
			String valueOf = String.valueOf(paraMap.get("name"));
			if(StringUtils.isNotBlank(valueOf)&& !"null".equals(valueOf)){
				modelMap.addAttribute("name",StringUtil.getByteToString(valueOf));
			}
	     	modelMap.addAttribute("data", (List<Map<String,Object>>)page.getResult());
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
     	}
		return "view/limits_list";
	}
	

	/**
	 * 根据数据库列名查询（如参数claim=id&&res=1即表示id为1的数据）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/power_claim")
	@ResponseBody
	public String power_claim(HttpServletRequest request) {
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		Map<String, Object> map = powerService.getPowerByclaim(pwsd);
		JSONObject jsonObject = JSONObject.fromObject(map);
		return String.valueOf(jsonObject);
	}

	/**
	 * 添加权限
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/power_add")
	@ResponseBody
	public String power_add(HttpServletRequest request) {
		int res = powerService.addPower(WebUtils.getParametersStartingWith(request, ""));
		return String.valueOf(res);
	}

	/**
	 * 修改权限
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/power_update")
	@ResponseBody
	public String power_update(HttpServletRequest request) {
		int res = powerService.updatePower(WebUtils.getParametersStartingWith(
				request, ""));
		return String.valueOf(res);
	}

}
